﻿// // ******************************************************************
// //       /\ /|       @file       FileHelper.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-12-28 20:12
// //    *(__\_\        @Copyright  Copyright (c) , 2024-12-27 18:12 Await
// // ******************************************************************

namespace AutomaticSubtitles.Service.Infrastructure.Helper;

public class FileHelper
{
    private static readonly List<string> ExtList = [".jpg", ".png", ".bmp"];

    /// <summary>
    /// 获取电视剧的海报，如果为空就是没有找到
    /// </summary>
    /// <param name="seriesDir"></param>
    /// <returns></returns>
    public static string? GetMovieLandscape(string seriesDir)
    {
        var movieRootDir = Path.GetDirectoryName(seriesDir);
        // 获取主封面
        foreach (var ext in ExtList)
        {
            // 1. backdrop.ext
            var posterFPath = Path.Combine(movieRootDir, "backdrop" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 2. landscape.ext
            posterFPath = Path.Combine(movieRootDir, "landscape" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 3. -thumb.ext
            posterFPath = movieRootDir + "-thumb" + ext;
            if (File.Exists(posterFPath)) return posterFPath;
        }

        return string.Empty; // 如果没有找到海报，返回空字符串
    }

    /// <summary>
    /// 获取电影的海报，如果为空就是没有找到
    /// </summary>
    /// <param name="movieFPath">路径</param>
    /// <returns></returns>
    public static string? GetMoviePoster(string movieFPath)
    {
        foreach (var ext in ExtList)
        {
            var movieRootDir = Path.GetDirectoryName(movieFPath);
            var movieName = Path.GetFileName(movieFPath);
            var movieNameWithoutExt = Path.GetFileNameWithoutExtension(movieName);

            // 1. poster.ext
            var posterFPath = Path.Combine(movieRootDir, "poster" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 2. movie.ext
            posterFPath = Path.Combine(movieRootDir, "movie" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 3. folder.ext
            posterFPath = Path.Combine(movieRootDir, "folder" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 4. <movie filename>-poster.ext
            posterFPath = Path.Combine(movieRootDir, movieNameWithoutExt + "-poster" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 5. <movie filename>.ext
            posterFPath = Path.Combine(movieRootDir, movieNameWithoutExt + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 6. cover.ext
            posterFPath = Path.Combine(movieRootDir, "cover" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 4. fanart.ext
            posterFPath = movieRootDir + "-thumb" + ext;
            if (File.Exists(posterFPath)) return posterFPath;
        }

        return string.Empty;
    }

    /// <summary>
    /// 获取电视剧的海报，如果为空就是没有找到
    /// </summary>
    /// <param name="seriesDir"></param>
    /// <returns></returns>
    public static string? GetSeriesPoster(string seriesDir)
    {
        // 获取主封面
        foreach (var ext in ExtList)
        {
            // 1. poster.ext
            var posterFPath = Path.Combine(seriesDir, "poster" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 2. folder.ext
            posterFPath = Path.Combine(seriesDir, "folder" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 3. fanart.ext
            posterFPath = Path.Combine(seriesDir, "fanart" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 4. thumb.ext
            posterFPath = seriesDir + "-thumb" + ext;
            if (File.Exists(posterFPath)) return posterFPath;
        }

        return string.Empty; // 如果没有找到海报，返回空字符串
    }

    /// <summary>
    /// 获取电视剧的横屏海报，如果为空就是没有找到
    /// </summary>
    /// <param name="seriesDir"></param>
    /// <returns></returns>
    public static string? GetSeriesLandscape(string seriesDir)
    {
        // 获取主封面
        foreach (var ext in ExtList)
        {
            // 1. backdrop.ext
            var posterFPath = Path.Combine(seriesDir, "backdrop" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 2. landscape.ext
            posterFPath = Path.Combine(seriesDir, "landscape" + ext);
            if (File.Exists(posterFPath)) return posterFPath;

            // 3. -thumb.ext
            posterFPath = seriesDir + "-thumb" + ext;
            if (File.Exists(posterFPath)) return posterFPath;
        }

        return string.Empty; // 如果没有找到海报，返回空字符串
    }

    /// <summary>
    /// 获取文件的大小是
    /// </summary>
    /// <param name="movieFPath"></param>
    /// <param name="len"></param>
    /// <returns></returns>
    public static decimal GetFileSize(string movieFPath, int len = 2)
    {
        try
        {
            var fileInfo = new FileInfo(movieFPath);

            if (!movieFPath.Contains("CERTIFICATE"))
            {
                var round = (decimal)fileInfo.Length;
                for (var i = 0; i <= len; i++) round /= 1024.0m;
                return Math.Round(round, 2);
            }

            var movieFPathFolder = Path.GetDirectoryName(movieFPath)!;
            return GetFolderSize(movieFPathFolder, len);
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    /// <summary>
    /// 获取文件夹的大小是
    /// </summary>
    /// <param name="movieFPath"></param>
    /// <param name="len"></param>
    /// <returns></returns>
    public static decimal GetFolderSize(string movieFPath, int len = 3)
    {
        var directoryInfo = new DirectoryInfo(movieFPath);
        var sum = (decimal)directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fileInfo => fileInfo.Length);
        for (var i = 0; i <= len; i++) sum /= 1024.0m;
        return Math.Round(sum, 2);
    }
}